package com.att.service.impl;

import com.att.bean.CheckExactBean;
import com.att.bean.CheckInOutBean;
import com.att.dao.CheckExactDaoMapper;
import com.att.dao.CheckInOutDaoMapper;
import com.att.service.CheckInOutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("checkInOutService")
public class CheckInOutServiceImpl implements CheckInOutService {

    @Resource
    private CheckInOutDaoMapper checkInOutDaoMapper;
    @Resource
    private CheckExactDaoMapper checkExactDaoMapper;

    @Override
    public List<CheckInOutBean> findByCondition(CheckInOutBean bean) {
        return checkInOutDaoMapper.findByCondition(bean);
    }

    @Override
    public List<CheckInOutBean> findByConditionCount(CheckInOutBean bean) {
        return checkInOutDaoMapper.findByConditionCount(bean);
    }

    @Override
    public boolean insert(CheckInOutBean bean) {
        boolean flag = checkInOutDaoMapper.insert(bean);
        if (flag) {
            CheckExactBean checkExactBean = new CheckExactBean();
            checkExactBean.setCreateTime(new Date());
            checkExactBean.setIsDelete(0);
            checkExactBean.setIsAdd(1);
            checkExactBean.setIsModify(0);
            checkExactBean.setStartTime(bean.getStartTime());
            checkExactBean.setEndTime(bean.getEndTime());
            checkExactBean.setCheckTime(bean.getCheckTime());
            checkExactBean.setUserId(bean.getUserID());
            checkExactBean.setYuanYin(bean.getYuanYin());
            checkExactBean.setCheckType(bean.getCheckType());
            List<CheckExactBean> exactBeanList = checkExactDaoMapper.findExactBeanList(checkExactBean);
            if (exactBeanList != null && exactBeanList.size() > 0) {
                for (int i = 0; i < exactBeanList.size(); i++) {
                    CheckExactBean exactBean = new CheckExactBean();
                    exactBean.setCheckTime(exactBeanList.get(i).getCheckTime());
                    exactBean.setCheckType(exactBeanList.get(i).getCheckType());
                    exactBean.setUserId(exactBeanList.get(i).getUserId());
                    exactBean.setYuanYin(exactBeanList.get(i).getYuanYin());
                    exactBean.setIsDelete(1);
                    exactBean.setIsAdd(0);
                    exactBean.setIsModify(0);
                    exactBean.setUserId(bean.getUserID());
                    exactBean.setCreateTime(new Date());
                    boolean flag1 = checkExactDaoMapper.insert(exactBean);
                    if (!flag1) {
                        return flag1;
                    }
                }
                boolean flag1 = checkExactDaoMapper.insert(checkExactBean);
                if (!flag1) {
                    return flag1;
                }
            } else {
                boolean flag1 = checkExactDaoMapper.insert(checkExactBean);
                if (!flag1) {
                    return flag1;
                }
            }
        }
        return flag;
    }

    @Override
    public boolean update(CheckInOutBean bean) {
        return checkInOutDaoMapper.update(bean);
    }

    @Override
    public boolean delete(Integer id) {
        return checkInOutDaoMapper.delete(id);
    }

    @Override
    public List<CheckInOutBean> findCheckInOutBeanList(CheckInOutBean bean) {
        return checkInOutDaoMapper.findCheckInOutBeanList(bean);
    }
}
